#include <iostream>

#include <QVector3D>
#include <QTextStream>

#include <libz800.h>

#include "DlgDisplay.h"

DlgDisplay::DlgDisplay(QWidget *parent)
: QDialog(parent)
{
	setupUi(this);
}

void DlgDisplay::setDevice(z800_device *dev)
{
	_dev = dev;

	int version = z800_get_firmware_version(_dev);

	if(version < 0)
	{
		qWarning("z800_get_firmware_version() failed");
		firmwareVersionLineEdit->setText("?");
	}
	else
	{
		firmwareVersionLineEdit->setText(QString::number(version >> 8) + "." + QString::number(version & 0xff));
	}
}

void DlgDisplay::on_wakePushButton_pressed()
{
	if(z800_wake(_dev) == false)
	{
		qWarning("z800_wake() failed");
	}
}

void DlgDisplay::on_reenableDisplayPushButton_pressed()
{
	if(z800_set_stereo_mode(_dev, stereoModePushButton->isChecked() ? Z800_TRUE : Z800_FALSE) == Z800_FALSE)
	{
		qWarning("z800_set_stereo_mode() failed");
		return;
	}
}

void DlgDisplay::on_stepBrightnessPushButton_pressed()
{
	if(z800_step_brightness(_dev) == Z800_FALSE)
	{
		qWarning("z800_step_brightness() failed");
	}
}

void DlgDisplay::setStereoEnabled(bool enabled)
{
	stereoModePushButton->setChecked(enabled);
	if(z800_set_stereo_mode(_dev, enabled ? Z800_TRUE : Z800_FALSE) == Z800_FALSE)
	{
		qWarning("z800_set_stereo_mode() failed");
		return;
	}
}

void DlgDisplay::on_stereoModePushButton_toggled(bool value)
{
	if(z800_set_stereo_mode(_dev, value ? Z800_TRUE : Z800_FALSE) == Z800_FALSE)
	{
		qWarning("z800_set_stereo_mode() failed");
		return;
	}

	emit stereoModeChanged(value);
}

void DlgDisplay::on_stereoFlipPushButton_pressed()
{
	if(z800_flip_stereo(_dev) == Z800_FALSE)
	{
		qWarning("z800_flip_stereo() failed");
	}
}
